import re
import os
userDataList = []
g_workPath = ""
def checkName(pattern,s):
    result = re.match(pattern,s)
    while True:
        if result:
            return result.group()
        print("信息格式不正确")
        newData = input("请重新输入要修改的信息：\n")
        result = re.match(pattern,newData)
def addUser():
    global userDataList
    name = input("请输入姓名：\n")
    name = checkName(r"\w*",name)

    age = input("请输入你的年龄：\n")
    age = checkName(r"[1-9]?[0-9]$",age)
    tel = input("请输入电话：\n")
    tel = checkName(r"^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(17[1-8])|(18[0,5-9]))\d{8}$",tel)
    data = (name,age,tel)
    userDataList.append(data)
    print("%s用户添加成功"%str(data))
def modifyUser():
    flag = int(input("选择修改选项，1姓名，2年龄，3电话\n"))
    findFlag = False
    name = input("选择要修改的用户姓名：\n")
    if 1 == flag:
        newName = input("输入新的用户名：\n")
        newName = checkName(r"\w*",newName)

        index = 0
        for data in userDataList:
            if data[0] == name:
                index = userDataList.index(data)
                del userDataList[index]
                newData = (newName,data[1],data[2])
                userDataList.insert(index,newData)
                findFlag = True
    elif 2 == flag:
        newAge = input("输入新的年龄：\n")
        newAge = checkName(r"[1-9]?[0-9]$", newAge)
        index = 0
        for data in userDataList:
            if data[0] == name:
                index = userDataList.index(data)
                del userDataList[index]
                newData = (data[0],newAge,data[2])
                userDataList.insert(index,newData)
                findFlag = True
    elif 3 == flag:
        newTEL = input("输入新的电话：\n")
        newTEL = checkName(r"^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(17[1-8])|(18[0,5-9]))\d{8}$"
                           , newTEL)
        for data in userDataList:
            if data[0] == name:
                index = userDataList.index(data)
                del userDataList[index]
                newData = (data[0],data[1],newTEL)
                userDataList.insert(index,newData)
                findFlag = True
    if findFlag:
        print("修改成功")
    else:
        print("不存在该用户或者输入信息错误")
def delUser():
    flag = int(input("请输入删除用户的方式，1姓名，2年龄，3电话：\n"))
    delFlag = False
    if 1 == flag:
        name = input("请输入要删除的用户名：\n")
        for data in userDataList:
            if data[0] == name:
                userDataList.remove(data)
                delFlag = True
    elif 2 == flag:
        age = input("请输入要删除用户的年龄：\n")
        for data in userDataList:
            if data[1] == age:
                userDataList.remove(data)
                delFlag = True
    else:
        tel = input("请输入要删除用户的电话：\n")
        for data in userDataList:
            if data[2] == tel:
                userDataList.remove(data)
                delFlag = True
    if delFlag:
        print("删除成功")
    else:
        print("用户不存在或者输入信息错误")

def readData():
    try:
        global userDataList
        file = open(g_workPath,'r')
        content = file.readlines()
        if content:
            for line in content:
                userDataList.append(eval(line))
        file.close()
    except:
        #os.mknod(r"E:\workspace\mypython\src\python5-18\test.txt")
        file = open(g_workPath,'w')
        file.close()
def writeData():
    file = open(g_workPath,'w')
    for data in userDataList:
        file.write(str(data)+"\n")
    file.close()
def showAllUser():
    for data in userDataList:
        print(data)
def getWorkPath():
    global g_workPath
    g_workPath = os.getcwd()
    g_workPath = os.path.join(os.getcwd(),"test.txt")

def main():
    getWorkPath()
    readData()
    while True:
        print("*"*40)
        choose = int(input("用户管理：1添加，2修改，3删除,4显示所有人,0退出\n"))
        if 1 == choose:
            addUser()
            showAllUser()
            writeData()
        elif 2 == choose:
            modifyUser()
            showAllUser()
            writeData()
        elif 3 == choose:
            delUser()
            showAllUser()
            writeData()
        elif 4 == choose:
            showAllUser()
        elif 0 == choose:
            break
        else:
            print("输入错误")
if __name__ == '__main__':
    main()


